package com.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    public void 分数排行()
    {
        ZSetOperations zset =  redisTemplate.opsForZSet();
        redisTemplate.delete("Math");
        zset.add("Math","张三",80);
        zset.add("Math","李四",90);
        zset.add("Math","王五",70);
        zset.add("Math","赵六",60);
        zset.add("Math","陈七",40);

        zset.add("Math","王八",30);
        zset.add("Math","王八",50);

        //BoundZSetOperations  set = redisTemplate.boundZSetOps("Math");
        //set.add("张三",90);
        long count = zset.size("Math");
        System.out.println("个数是:"+count);

        //按分数升序输出名称
        Set<String> set = zset.range("Math",0, count-1);
        for (String s : set) {
            System.out.println(s);
        }

        System.out.println("--------------------------------------");
        System.out.println("分数最高三人，还要显示名字和成绩");
        Set<ZSetOperations.TypedTuple> set2 = zset.reverseRangeWithScores("Math",0,2);
        for (ZSetOperations.TypedTuple s : set2) {
            System.out.println(s.getValue()+"\t"+s.getScore());
        }
        System.out.println("-------------------------------------------------");
        System.out.println("输出分数是60-80间所有学生姓名和分数，降序");
        Set<ZSetOperations.TypedTuple>  set3= zset.reverseRangeByScoreWithScores("Math",60,80);

        int c = 0;
        for (ZSetOperations.TypedTuple t : set3) {
            c++;
            if (c>3)
            {
                break;
            }
            System.out.println(t.getValue()+"\t"+t.getScore());
        }

        System.out.println("-----------------------------------");
        System.out.println("判断某个人是否在集合中");
        Long minchi = zset.rank("Math","张三");
        if (minchi==null)
        {
            System.out.println("查无此人");
        }
        else
        {
            System.out.println("找到了，下标是:"+minchi);
        }

        //删除
        //zset.remove("Math","张三");
        //zset.removeRangeByScore("Math",60,80);





    }



}
